pp108 : Rules on XML to JSON Conversion

Rules on XML to JSON Conversion

The default XML to JSON conversion rules are as below. 

Pattern Description

XML

JSON

Access

Empty Element

<Name></Name>

 Name:""

 

Element with text and no other children

<Name>Alchemist</Name>

 Name:"Alchemist"

 

Element with children 1

<Book><Name>Alchemist</Name><Author>Paulo Coehlo</Author></Book>

Book:{
 Name:"Alchemist",
 Author:"Paulo Coehlo"
 } 

Accessing Book Id and Book Author

 Book.Name 
 Book.Author

Element with children 2

<Book id="ISBN:1321324354436"> <Name>Alchemist</Name> <Author country="Brazil">Paulo Coehlo</Author>
</Book>

Book:{
"@id":"ISBN:1321324354436",
"Name":"Alchemist",
Author:
{ "text":"Paulo Coehlo", "@country":"Brazil" }
}

Accessing Book Id and Book name

 Book["@id"] 
 Book.Name

Element with attributes and text

<Book><Author country="Brazil">Paulo Coehlo</Author><Book>

Book:{
  Author:
  { text:"Paulo Coehlo", "@country":"Brazil" }
}

Accessing Author and Author Country

 Book.Author.text
 Book.Author.["@country"]

Namespace (are also considered attributes)

<Author country="Brazil" xmlns="http://sample.com/demo">Paulo Coehlo</Author>

Author:
{  "text":"Paulo Coehlo",
"@country":"Brazil", 
"@xmlns":"http://sample.com/demo"}

Accessing name space for Author

  Author["@xmlns"] 

Element with prefix

<Book><demo:Author country="Brazil" xmlns:demo="http://sample.com/demo">Paulo Coehlo</demo:Author> <Book>

Book:{
"demo:Author":{ "text":"Paulo Coehlo", "@country":"Brazil",
 "@xmlns" : "http://sample.com/demo" }
}

Accessing Author text and Country

Book["demo:Author"].text
Book["demo:Author"]["@country"]
 

null="true"

<Name null="true" xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 

Name:""

 

Arrays

<Books><Book><Name>The Prophet</Name> <Author>Kahlil Gibran</Author></Book><Book><Name>Alchemist</Name><Author>Paulo Coehlo</Author></Book> <Book><Name>Tuesdays with Morrie</Name><Author>Mitch Albom</Author></Book></Books>

Books:{Book:[
{ Name:"The Prophet", Author:"Kahlil Gibran" },
{ Name:"Alchemist", Author:"Paulo Coehlo"},
{ Name:"Tuesdays with Morrie", Author:"Mitch Albom"}
]}

 

Arrays without Child <Books><Book>A</Book><Book>B</Book><Book>C</Book></Books>
Books:{
Book:["A","B","C"]}
}